package com.huxiu.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huxiu.HuaTiDetailActivity;
import com.huxiu.R;
import com.huxiu.listener.ListenerConstans;
import com.huxiu.listener.ViewPagerListener;
import com.huxiu.net.model.HuaTiItem;
import com.huxiu.utils.Constants;
import com.huxiu.utils.ImageLoaderOptions;
import com.huxiu.utils.LogUtil;
import com.huxiu.utils.Utils;
import com.huxiu.view.RefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.lang.ref.WeakReference;


public class HuaTiHolder implements OnClickListener {

    private ViewPagerListener mViewPagerListener;
    @ViewInject(R.id.item_mine_qunzu_title)
    private LinearLayout titleMineItem;

    //    @ViewInject(R.id.huati_item_frame)
    //    private FrameLayout frame;

    @ViewInject(R.id.huati_item_title)
    private TextView title;

    @ViewInject(R.id.huati_item_img)
    private ImageView imgage;

    @ViewInject(R.id.huati_item_name)
    private TextView name;

    @ViewInject(R.id.huati_item_liulan_num)
    private TextView liulanNum;

    @ViewInject(R.id.huati_item_time)
    private TextView time;

    @ViewInject(R.id.huati_item_re)
    private TextView remen;


    @ViewInject(R.id.item_huati_ll)
    private LinearLayout item_huati_ll;

    @ViewInject(R.id.qunzu_mine_nolist)
    private RelativeLayout qunzu_mine_nolist;

    @ViewInject(R.id.button_faxian_qunzu)
    private Button button_faxian_qunzu;
    @ViewInject(R.id.view_line)
    private View view_line;


    private static Activity mContext;
    private int position = -1;
    private RefreshListView lv_tabdetail_list;
    private static WeakReference<BaseAdapter> mAdapter;

    public void setAdapter(BaseAdapter adapter) {
        mAdapter = new WeakReference<>(adapter);
    }

    public HuaTiHolder(View view, Context context) {
        ViewUtils.inject(this, view);
        mContext = (Activity) context;
        if (ListenerConstans.mQunZuPager != null) {
            mViewPagerListener = ListenerConstans.mQunZuPager;
        }

    }


    private HuaTiItem mItem;

    public void bindItem() {
        button_faxian_qunzu.setOnClickListener(this);
        titleMineItem.setVisibility(View.VISIBLE);
        qunzu_mine_nolist.setVisibility(View.VISIBLE);
        item_huati_ll.setVisibility(View.GONE);
        view_line.setVisibility(View.GONE);
    }

    public void bindItem(HuaTiItem item, int position) {
        item_huati_ll.setOnClickListener(this);
        mItem = item;
        if (position == 0) {
            titleMineItem.setVisibility(View.VISIBLE);
        } else {
            titleMineItem.setVisibility(View.GONE);
        }
        String times;
        remen.setVisibility(View.GONE);
        liulanNum.setText(Utils.getSeeNumToStr(mItem.view_num) + "人浏览");
        times = Utils.getDateString(mItem.time);
        time.setText(times);
        name.setText(mItem.userinfo.username);
        title.setText(mItem.title);
        ImageLoader.getInstance().displayImage(
                mItem.userinfo.avatar + Constants.IMAGE_WEBP, imgage, ImageLoaderOptions.options);
    }

    public void bindItem(HuaTiItem item) {
        item_huati_ll.setOnClickListener(this);
        mItem = item;
        String times;
        if (mItem.label == 2) {
            remen.setVisibility(View.VISIBLE);
            remen.setText("荐");
            remen.setTextColor(0xff0da3fa);
        } else if (mItem.label == 1) {
            remen.setText("热");
            remen.setTextColor(0xfff63756);
            remen.setVisibility(View.VISIBLE);
        } else {
            remen.setVisibility(View.GONE);
        }
        liulanNum.setText(Integer.toString(mItem.view_num) + "人浏览");
        times = Utils.getDateString(mItem.time);
        time.setText(times);
        name.setText(mItem.userinfo.username);
        title.setText(mItem.title);
        ImageLoader.getInstance().displayImage(
                mItem.userinfo.avatar + Constants.IMAGE_WEBP, imgage, ImageLoaderOptions.options);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_huati_ll:
                Intent intent = new Intent(mContext, HuaTiDetailActivity.class);
                intent.putExtra(Constants.GROUP_HUATI_ITEM_ID, mItem.id);
                mContext.startActivity(intent);
                break;
            case R.id.button_faxian_qunzu:
                if (mViewPagerListener != null) {
                    mViewPagerListener.setCurrentItem(1, ListenerConstans.mFaXian);
                }
                break;
        }
    }

}
